package com.ecoolex.hotel.saas.admin.graphql;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.BeanUtil;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.modularize.api.service.storage.FileSize;
import com.ecoolex.framework.modularize.api.service.storage.MimeType;
import com.ecoolex.framework.modularize.api.service.storage.StorageInfo;
import com.ecoolex.framework.modularize.api.service.storage.StorageService;
import com.ecoolex.framework.modularize.api.service.storage.Upload;
import com.ecoolex.framework.modularize.api.service.storage.UploadLimit;
import com.ecoolex.framework.modularize.api.service.storage.UploadSource;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;
import com.ecoolex.hotel.saas.admin.core.AdminConstant;
import com.ecoolex.hotel.saas.entity.StoreBrand;
import com.ecoolex.hotel.saas.pojo.AddStoreBrandDTO;
import com.ecoolex.hotel.saas.pojo.EditStoreBrandDTO;
import com.ecoolex.hotel.saas.ws.StoreService;

@GraphQL2Module(type = Type.Admin, description = "商户品牌")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_PROJECT_KEY,
	key = BrandGraphql.AUTH,
	name = "商户品牌管理",
	remark = "商户品牌管理")
public class BrandGraphql {

	public static final String AUTH = AdminConstant.AUTH_PROJECT_KEY + "-BRAND";
	
	private static final String AUTH_GET = AUTH + "-GET";
	
	private static final String AUTH_QUERY = AUTH + "-QUERY";
	
	private static final String AUTH_ADD = AUTH + "-ADD";
	
	private static final String AUTH_EDIT = AUTH + "-EDIT";
	
	private static final String AUTH_DEL = AUTH + "-DEL";
	
	@Autowired
	private StorageService storageService;
	
	@Reference
	private StoreService service;
	
	@TypeField(type = StoreBrand.class, field = "canEdit", description = "是否能编辑")
	public Boolean canEdit(AdminMemberAuthority user) {
		return user.auth(AUTH_EDIT);
	}

	@TypeField(type = StoreBrand.class, field = "canDel", description = "是否能删除")
	public Boolean canDel(AdminMemberAuthority user) {
		return user.auth(AUTH_DEL);
	}
	
	@TypeField(type = StoreBrand.class, field = "logo", description = "Logo")
	public StorageInfo logo(@Source StoreBrand brand) {
		return storageService.get(brand.getLogo());
	}
	
	@QueryField(field = "storeBrand", description = { "商户品牌详情" })
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "获取商户品牌详情")
	public StoreBrand get(@NonNull Integer brandId, AdminMemberAuthority user) {
		user.assertAuth(AUTH_GET);
		return service.getStoreBrand(brandId);
	}
	
	@QueryField(field = "storeBrandList", description = { "商户品牌列表" })
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY, name = "查询商户品牌列表")
	public Pagination<StoreBrand> query(String brandName, @NonNull Page page, AdminMemberAuthority user) {
		user.assertAuth(AUTH_QUERY);
		return service.queryStoreBrand(brandName, page);
	}
	
	@MutationField(field = "uploadBrandLogo", description = { "上传商户品牌Logo" })
	public StorageInfo uploadGoodsBrandLogo(@NonNull Upload request, AdminMemberAuthority user) {
		if (!user.auth(AUTH_ADD) && !user.auth(AUTH_EDIT)) {
            user.notAuthority();
        }
        UploadSource uploadSource = new UploadSource("StoreBrand", "Logo");
        UploadLimit uploadLimit = new UploadLimit();
        uploadLimit.setMaxSize(FileSize.MB.byteLength(10));
        uploadLimit.setAllowMimeType(MimeType.IMAGE.commons());

        return storageService.upload(request, uploadSource, uploadLimit);
	}
	
	@MutationField(field = "addStoreBrand", description = { "添加商户品牌" })
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "添加商户品牌")
	public StoreBrand add(@NonNull AddStoreBrandDTO add, AdminMemberAuthority user) {
		user.assertAuth(AUTH_ADD);
		Assert.isTrue(storageService.has(add.getLogo()), ResultCode.VERIFY.build(1), "logo不存在");
		Assert.isTrue(Check.notNullOrTrimEmpty(add.getName()), ResultCode.VERIFY.build(2), "名称为空");
		Assert.isTrue(Check.notNull(add.getWeight()), ResultCode.VERIFY.build(3), "权重为空");
		return service.addStoreBrand(add, user.userName());
	}
	
	@MutationField(field = "editStoreBrand", description = { "编辑商户品牌" })
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT, name = "编辑商户品牌")
	public StoreBrand edit(@NonNull EditStoreBrandDTO edit, AdminMemberAuthority user) {
		user.assertAuth(AUTH_EDIT);
		Assert.isTrue(storageService.has(edit.getLogo()), ResultCode.VERIFY.build(1), "logo不存在");
		Assert.isTrue(Check.notNullOrTrimEmpty(edit.getName()), ResultCode.VERIFY.build(2), "名称为空");
		Assert.isTrue(Check.notNull(edit.getWeight()), ResultCode.VERIFY.build(3), "权重为空");
		Assert.isTrue(Check.notNull(edit.getBrandId()), ResultCode.VERIFY.build(4), "品牌id为空");
		StoreBrand record = service.getStoreBrand(edit.getBrandId());
		Assert.isTrue(Check.notNull(record), ResultCode.VERIFY.build(1), "品牌不存在");
		BeanUtil.copy(edit, record);
		return service.editStoreBrand(record, user.userName());
	}

	@MutationField(field = "delStoreBrand", description = { "删除商户品牌" })
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_DEL, name = "删除商户品牌")
	public Boolean delete(@NonNull Integer brandId, AdminMemberAuthority user) {
		user.assertAuth(AUTH_DEL);
		Assert.isTrue(Check.notNull(brandId), ResultCode.VERIFY.build(1), "品牌id为空");
		StoreBrand record = service.getStoreBrand(brandId);
		Assert.isTrue(Check.notNull(record), ResultCode.VERIFY.build(1), "品牌为空");
		return service.delStoreBrand(record, user.userName());
	}
}
